package com.example.admin.brainwave.ui.activity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.media.MediaPlayer;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.admin.brainwave.MyApp;
import com.example.admin.brainwave.R;
import com.example.admin.brainwave.base.BaseActivity;
import com.example.admin.brainwave.bean.CurrencyBean;
import com.example.admin.brainwave.net.SaveTestDataTask;
import com.example.admin.brainwave.utils.AudioUtils;
import com.example.admin.brainwave.utils.ToastUtil;

import java.util.Set;

import butterknife.BindView;

public class BrainCollectionHomeActivity extends BaseActivity {


    @BindView(R.id.iv_top_back)
    ImageView ivTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.iv_top_login_out)
    ImageView ivTopLoginOut;
    @BindView(R.id.tv_top_right)
    TextView tvTopRight;
    @BindView(R.id.iv_top_connect)
    ImageView ivTopConnect;
    @BindView(R.id.rl_collection_title)
    RelativeLayout rlCollectionTitle;
    @BindView(R.id.rl_collection_text)
    RelativeLayout rlCollectionText;
    @BindView(R.id.btn_collection_next)
    Button btnCollectionNext;
    @BindView(R.id.tv_collection_text)
    TextView tvCollectionText;

    int flag;
    @BindView(R.id.btn_collection_look)
    Button btnCollectionLook;
    @BindView(R.id.btn_collection_nolook)
    Button btnCollectionNolook;
    @BindView(R.id.ll_collection_next)
    LinearLayout llCollectionNext;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_brain_collection_home;
    }

    @Override
    protected void initView() {
        tvTopTitle.setText(getResources().getText(R.string.brain_collection));
        ivTopConnect.setVisibility(View.VISIBLE);
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, "当前设备不支持蓝牙功能", Toast.LENGTH_SHORT).show();
            finish();
        }
        if (!mBluetoothAdapter.isEnabled()) {
            Toast.makeText(this, "蓝牙没有打开", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, BluetoothActivity.class));
            finish();
        }
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() == 0) {
            Toast.makeText(this, "设备没有配对", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, BluetoothActivity.class));
            finish();
        }
        flag = getIntent().getIntExtra("flag", 0);
        if (flag == 0) {
            MyApp.getDaoSession().getDataDB1Dao().deleteAll();
            MyApp.getDaoSession().getDataDB2Dao().deleteAll();
            llCollectionNext.setVisibility(View.GONE);
            btnCollectionNext.setVisibility(View.VISIBLE);
            tvCollectionText.setText(getResources().getString(R.string.collection1));
            btnCollectionNext.setText(getResources().getText(R.string.got_it));
            AudioUtils.playSound(this, R.raw.audio1, mp -> {
            });
        } else if (flag == 2) {
            llCollectionNext.setVisibility(View.GONE);
            btnCollectionNext.setVisibility(View.VISIBLE);
            tvCollectionText.setText(getResources().getString(R.string.collection3));
            btnCollectionNext.setText(getResources().getText(R.string.can_continue));
            AudioUtils.playSound(this, R.raw.audio3, mp -> {
            });
        } else if (flag == 3) {
            llCollectionNext.setVisibility(View.VISIBLE);
            btnCollectionNext.setVisibility(View.GONE);
            tvCollectionText.setText(getResources().getString(R.string.collection4));
            new SaveTestDataTask(this, o -> {
                if (o != null) {
                    CurrencyBean c = (CurrencyBean) o;
                    ToastUtil.showShort(BrainCollectionHomeActivity.this, c.getMessage());
                    AudioUtils.playSound(BrainCollectionHomeActivity.this, R.raw.audio4, (MediaPlayer mp) -> {
                    });
                }
            }).execute();
        }
    }


    @Override
    protected void addListener() {
        ivTopBack.setOnClickListener(view -> finish());
        ivTopConnect.setOnClickListener(view -> startActivity(new Intent(BrainCollectionHomeActivity.this, BluetoothActivity.class)));
        btnCollectionNext.setOnClickListener(view -> {
            if (flag == 0) {
                tvCollectionText.setText(getResources().getString(R.string.collection2));
                btnCollectionNext.setText(getResources().getString(R.string.be_ready));
                AudioUtils.playSound(BrainCollectionHomeActivity.this, R.raw.audio2, mp -> {
                });
                flag = 1;
            } else {
                AudioUtils.pause();
                Intent intent = new Intent(BrainCollectionHomeActivity.this, BrainCollectionActivity.class);
                intent.putExtra("flag", flag);
                startActivity(intent);
                finish();
            }
        });
        btnCollectionLook.setOnClickListener(view -> {
            Intent intent = new Intent(BrainCollectionHomeActivity.this, ResultActivity.class);
            startActivity(intent);
            finish();
        });

        btnCollectionNolook.setOnClickListener(view -> finish());
    }

    @Override
    protected void onPause() {
        AudioUtils.pause();
        super.onPause();
    }
}
